package com.cn.xact.ty_treasured_book.activity;

import com.cn.xact.ty_treasured_book.R;
import com.cn.xact.ty_treasured_book.net.NetworkCallBack;
import com.cn.xact.ty_treasured_book.net.NetworkImpl;
import com.cn.xact.ty_treasured_book.util.UIHelper;
import com.cn.xact.ty_treasured_book.TYUserState;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class HomeSettingsActivity extends Activity {

    private EditText mNickName = null;
    private RadioGroup mGenderRadio = null;
    private int mCurGender = -1;
    private NetworkImpl mSettingsNetwork;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.activity_home_settings);

        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.common_customer_title);

        ImageView tmp = (ImageView)findViewById(R.id.common_title_indicator);
        tmp.setImageResource(R.drawable.main_home);
        TextView title1 = (TextView)findViewById(R.id.common_title_first_section);
        title1.setText(R.string.title_activity_home);
        TextView separator1 = (TextView)findViewById(R.id.common_title_first_separator);
        separator1.setVisibility(View.VISIBLE);
        TextView title2 = (TextView)findViewById(R.id.common_title_second_section);
        title2.setText(R.string.home_user_setting);
        title2.setVisibility(View.VISIBLE);

        title1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                HomeSettingsActivity.this.finish();
            }

        });

        mSettingsNetwork = new NetworkImpl(this, new MyNetworkCallBack());

        mNickName = (EditText)findViewById(R.id.home_user_settings_modify_nick_name);
        mNickName.setHint(TYUserState.getUserNick());

        mGenderRadio = (RadioGroup)findViewById(R.id.home_user_settings_gender_radio_group);

        int defaultId = -1;
        mCurGender = TYUserState.getUserGender();
        if (mCurGender == 1) {
            defaultId = R.id.home_user_settings_gender_male;
        } else {
            defaultId = R.id.home_user_settings_gender_female;
        }
        mGenderRadio.check(defaultId);
        mGenderRadio.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup arg0, int arg1) {
                // TODO Auto-generated method stub
                if (arg1 == R.id.home_user_settings_gender_male) {
                    mCurGender = 1;
                } else {
                    mCurGender = 0;
                }
            }

        });

        Button okbt = (Button)findViewById(R.id.home_user_settings_confirmed);
        okbt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                mSettingsNetwork.setUserInfoAsync(123, mCurGender, mNickName.getText().toString(), 0);
            }

        });

        Button cancelbt = (Button)findViewById(R.id.home_user_settings_canceld);
        cancelbt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                HomeSettingsActivity.this.finish();
            }

        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home_settings, menu);
        return true;
    }

    private void showComplete() {
        AlertDialog.Builder build = new AlertDialog.Builder(this);
        build.setMessage(getResources().getString(R.string.home_user_settings_succeed));
        build.setPositiveButton(getResources().getString(R.string.OK), new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                HomeSettingsActivity.this.finish();
            }
        });

        build.show();
    }

    private void showFailed() {
        AlertDialog.Builder build = new AlertDialog.Builder(this);
        build.setMessage(getResources().getString(R.string.home_user_settings_failed));
        build.setPositiveButton(getResources().getString(R.string.OK), new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        build.show();
    }

    class MyNetworkCallBack extends NetworkCallBack {
        public void onNetworkCallBack(int id, boolean success, Object attach1, Object attach2) {
            if (!success) {
                UIHelper.showConfirmDialog(HomeSettingsActivity.this, null, getResources().getString(R.string.server_busy));
                return;
            }
            switch(id) {
            case NetworkImpl.NET_WORK_ID_SET_USER_INFO:
                if (success) {
                    TYUserState.updateUserState(
                            TYUserState.getUserLoginCount()
                            , TYUserState.getUserCredit()
                            , TYUserState.getUserRank()
                            , mCurGender
                            , null
                            , mNickName.getText().toString());
                    showComplete();
                } else {
                    showFailed();
                }
                break;
            }
        }
    }
}
